//
//  SecondViewController2.m
//  UIhomework_14
//
//  Created by dllo on 16/1/2.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "SecondViewController2.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "DataBaseHandle.h"
@interface SecondViewController2 ()

@end

@implementation SecondViewController2

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
    
}
-(void)dealloc{
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = itemLeft;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction1:)];
    self.navigationItem.rightBarButtonItem = item1;
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:scroll];
    scroll.contentSize = CGSizeMake(0, self.view.frame.size.height * 1.2);
    scroll.bounces = NO;
    scroll.pagingEnabled = YES;
    scroll.showsVerticalScrollIndicator = NO;
    [scroll release];
    
    MovieModel *model = _modelArr[_index.row];
    self.navigationItem.title = model.title;
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 94, 200)];
    [imgview sd_setImageWithURL:[NSURL URLWithString:model.image]];
    [scroll addSubview:imgview];
    [imgview release];
    
    UILabel *labelScore = [[UILabel alloc] initWithFrame:CGRectMake(145, 20, 200, 20)];
    labelScore.text = [NSString stringWithFormat:@"评分：%@ （%@评论)", model.rating, model.wish];
    [scroll addSubview:labelScore];
    [labelScore release];
    
    UILabel *labelDate = [[UILabel alloc] initWithFrame:CGRectMake(145, 50, 200, 20)];
    labelDate.text = [NSString stringWithFormat:@"%@", model.pubdate];
    [scroll addSubview:labelDate];
    [labelDate release];
    
       
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)itemAction:(UIBarButtonItem *)item{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)itemAction1:(UIBarButtonItem *)item1{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//    [alert addAction:action];
//    [self presentViewController:alert animated:YES completion:nil];
   
    
    //查询电影数据，判断电影表单里头是否含有当前标题
   NSArray *arr = [[DataBaseHandle shareDataBaseHandle] selectMovieTable];
    
    NSMutableArray *arrModel = [NSMutableArray array];
    
    for (MovieModel *model in arr) {
        NSString *str = [NSString stringWithFormat:@"%@", model.title];
        [arrModel addObject:str];
    }
    
    if (![arrModel containsObject:self.navigationItem.title]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
           [alert addAction:action];
           [self presentViewController:alert animated:YES completion:nil];
        
        
        
        //插入电影数据
        MovieModel *model = [MovieModel movieModel];
        model.title = self.navigationItem.title;
        [[DataBaseHandle shareDataBaseHandle] insertMovieModel:model];

    } else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];

    }
    
    

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
